﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using NIIT.SaoBacDau.Aupi.HospitalManagement.Entities;
using NIIT.SaoBacDau.Aupi.HospitalManagement.BusinessLogic;
using NIIT.SaoBacDau.Aupi.HospitalManagement.Languages;

namespace NIIT.SaoBacDau.Aupi.HospitalManagement
{
    public partial class LoginSystems : Form
    {
        public Boolean bln_RememberPassword;

        public int status;

        public Obj_Login log;

        public LoginSystems()
        {
            InitializeComponent();
            this.status = 0;
        }

        public LoginSystems(Obj_Login objlog,int logout)
        {
            InitializeComponent();
            this.log = objlog;
            this.status = logout;
        }

        private void tbx_UserName_KeyUp(object sender, KeyEventArgs e)
        {
            if (tbx_UserName.Text.Length > 4)
                btn_Login.Enabled = true;
            else
                btn_Login.Enabled = false;
        }

        private void tbx_Password_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
                LoginHM();
        }

        private void btn_Login_Click(object sender, EventArgs e)
        {
            RememberPass();
            LoginHM();
        }

        private void RememberPass()
        {
            if (cbx_RememberPassword.Checked == true)
                bln_RememberPassword = true;
            else
                bln_RememberPassword = false;
        }

        private void btn_Reset_Click(object sender, EventArgs e)
        {
            tbx_UserName.Text = null;
            tbx_Password.Text = null;
            cbx_RememberPassword.Checked = false;
        }

        private void btn_Exit_Click(object sender, EventArgs e)
        {
            LibraryLanguages.ExitLogin();
        }

        private void LoginSystems_Load(object sender, EventArgs e)
        {
            if (log == null)
                tbx_UserName.Focus();
            else
            {
                tbx_UserName.Text = log.UserName;
                tbx_Password.Text = log.Password;
                tbx_UserName.Focus();
            }
        }

        private void LoginHM()
        {
            Obj_Login Ac = new Obj_Login();
            Ac.UserName = tbx_UserName.Text.ToString();
            Ac.Password = tbx_Password.Text.ToString();
            CheckAccount(Ac,bln_RememberPassword);
        }

        private void CheckAccount(Obj_Login Ac,bool bln_RememberPassWord)
        {
                LoginError Error = BL_Logins.Login(Ac, bln_RememberPassword,status);
                switch (Error)
                {
                    case LoginError.FormatUserName:
                        Languages.LibraryLanguages.UserNameIsNotFormat();
                        tbx_UserName.Text = "";
                        tbx_UserName.Focus();
                        break;
                    case LoginError.PasswordIsEmpty:
                        Languages.LibraryLanguages.PasswordIsEmpty();
                        tbx_Password.Text = "";
                        tbx_Password.Focus();
                        break;
                    case LoginError.FormatPassword:
                        Languages.LibraryLanguages.PasswordIsNotFormat();
                        tbx_Password.Text = "";
                        tbx_Password.Focus();
                        break;
                    case LoginError.NotAccount:
                        Languages.LibraryLanguages.NotAccount();
                        tbx_UserName.Text = "";
                        tbx_Password.Text = "";
                        tbx_UserName.Focus();
                        break;
                    case LoginError.NoError:
                        ShowSystem(Ac);
                        break;
                    default:
                        break;

                }
        }

        private void ShowSystem(Obj_Login Ac)
        {
            if (status == 0)
            {
                BL_Logins.WellComeEmployee(Ac);
                this.Hide();
            }
            else if (status == 1)
            {
                Managements app = new Managements(log, BL_Logins.WellComeEmployee(Ac));
                app.ShowDialog();
            }
        }
    }
}
